package com.hspedu.newData.dmsxl.图;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-05-28 
 * @Version: 1.0
 */

public class LC841钥匙和房间 {

    public boolean canVisitAllRooms(List<List<Integer>> rooms) {
        int len = rooms.size();

        boolean[] visited = new boolean[len];

        dfs(rooms,0,visited);

        for (int i = 0; i < len; i++) {
            if (!visited[i]){
                return false;
            }
        }
        return true;
    }

    public void dfs(List<List<Integer>> rooms,int key,boolean[] visited){

        if (visited[key] == true){
            return;
        }
        visited[key] = true;
        List<Integer> keys = rooms.get(key);
        for (Integer newKey : keys) {
            dfs(rooms,newKey,visited);
        }
    }

    @Test
    public void test() {
        // 测试代码

        List<List<Integer>> list = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        path.add(1);
        list.add(new ArrayList<>(path));
        List<Integer> path1 = new ArrayList<>();
        path.add(2);
        list.add(new ArrayList<>(path1));
        List<Integer> path2 = new ArrayList<>();
        path.add(3);
        list.add(new ArrayList<>(path2));
        List<Integer> path3 = new ArrayList<>();
        list.add(new ArrayList<>(path3));
        canVisitAllRooms(list);
    }
}
